home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2364 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: rose.etri.re.kr!jskim
  2. From: jskim@rose.etri.re.kr (Kim Jungsun)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: problems w/ relational operator ==
  5. Date: 17 Jan 1996 08:50:10 GMT
  6. Organization: ETRI
  7. Distribution: world
  8. Message-ID: <4did82$lm4@ns.etri.re.kr>
  9. References: <30FC83D2.7B6A@hti.net>
  10. NNTP-Posting-Host: rose1.etri.re.kr
  11. Keywords: precision
  12.  
  13. In article <30FC83D2.7B6A@hti.net>, Michael Benrud <mbenrud@hti.net> writes:
  14. |> I am having some problems using the == operator. I am using BC 4.02.
  15. |> I have the following snipet of code:
  16. |> 
  17. |> double i;
  18. |> 
  19. |> if(i == 1.0)
  20. |>   Xinc = Xinc * 10;
  21. |> 
  22. |> The problem is that when i equals 1.0, the if statement never executes 
  23. |> Xinc = Xinc * 10. Any ideas? Thanks in advance.
  24.  
  25.  
  26. Generally, it is a bad idea to do an equality check between non-int numbers,
  27. because their might be a round off error. To compare two double numbers to
  28. check an equality, I normally do it this way:
  29.  
  30. #define BOUND 0.0000001     // It is your responsibility to set
  31.                 // this value appropriately
  32.  
  33. inline bool IS_ZERO(double val)
  34. {
  35.    return ((val - 0.0) < BOUND) && ((val - 0.0) > -BOUND) ? 1 : 0;
  36. }
  37.  
  38. ...
  39.  
  40. if (IS_ZERO(i - 1.0)) {
  41.     Xinc = Xinc * 10;
  42.  
  43.  
  44. +=====================================================================+
  45. | Jungsun Kim, PhD                 Parallel Programming Section       |
  46. |                                  System Research Department         |
  47. | Email: jskim@rose.etri.re.kr     Electronics And Telecommunications |
  48. | Phone : +82-42-860-6699              Research Institute             |
  49. | Fax   : +82-42-860-6645          P.O.Box 106, Yusong                |
  50. |                                  Daejon, 305-600, KOREA             |
  51. +=====================================================================+
  52.  
  53.  
  54.  
  55.  
  56.  
  57.